﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace Chart.Net.Demo
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }

        private void btnDemo_Click(object sender, EventArgs e)
        {
            Report report = new Report();
            report.Title = "Asp.Net Slow Response Report";
            ChartFactory factory = null;
            switch (cmbProvider.Text)
            {
                case "Raphael":
                    factory = new RaphaelChartFactory();
                    break;
                case "JqPlot":
                    factory = new JqPlotChartFactory();
                    break;
                default:
                    factory = new JqPlotChartFactory();
                    break;
            }

            if (chkPlainText.Checked)
                InsertPlainTextEntity(factory, report);
            if (chkTable.Checked)
                InsertTableEntity(factory, report);
            if (chkBar.Checked)
            {
                InsertBarEntity(factory, report);
                InsertMultiBarEntity(factory, report);
            }
            if (chkPie.Checked)
                InsertPieEntity(factory, report);

            string partialPath = report.Save();
            string path = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\\')+1)+partialPath;
            Process.Start("iexplore.exe",path);
        }

        #region Demo Entity Creation

        private void InsertPlainTextEntity(ChartFactory factory, Report report)
        {
            PlainText threadpoolEntity = factory.CreatePlainText();
            threadpoolEntity.Title = "Thread Pool Summary";
            threadpoolEntity.Content = @"
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new mainForm());
        }
    }";
            report.Entities.Add(threadpoolEntity);
        }

        private void InsertTableEntity(ChartFactory factory, Report report)
        {
            TableChart executingRequestEntity = factory.CreateTableChart();
            executingRequestEntity.Title = "Executing ASP.NET Requests";
            executingRequestEntity.Columns.Add(new TableChart.Column("HttpContext"));
            executingRequestEntity.Columns.Add(new TableChart.Column("Timeout"));
            executingRequestEntity.Columns.Add(new TableChart.Column("Completed"));
            executingRequestEntity.Columns.Add(new TableChart.Column("ThreadId"));
            executingRequestEntity.Columns.Add(new TableChart.Column("ReturnCode"));
            executingRequestEntity.Columns.Add(new TableChart.Column("Verb"));
            executingRequestEntity.Columns.Add(new TableChart.Column("RequestPath+QueryString"));
            for (int i = 0; i < 20; i++)
            {
                TableChart.Row row = executingRequestEntity.NewRow();
                for (int j = 0; j < executingRequestEntity.Columns.Count; j++)
                {
                    row[j].CellValue = "test value";
                }
            }
            report.Entities.Add(executingRequestEntity);
        }

        private void InsertBarEntity(ChartFactory factory, Report report)
        {
            BarChart barEntity = factory.CreateBarChart();
            barEntity.Title = "Memory Bar";
            barEntity.BarCollection.Add(new List<BarChart.Bar>());
            for (int i = 0; i < 5; i++)
            {
                barEntity.BarCollection[0].Add(new BarChart.Bar("string", "100"));
                barEntity.BarCollection[0].Add(new BarChart.Bar("connection", "50"));
                barEntity.BarCollection[0].Add(new BarChart.Bar("datatable", "10"));
            }
            report.Entities.Add(barEntity);
        }

        private void InsertMultiBarEntity(ChartFactory factory, Report report)
        {
            BarChart multipleBarEntity = factory.CreateBarChart();
            multipleBarEntity.Title = "Memory Bar1";
            multipleBarEntity.BarHeight = 20;

            multipleBarEntity.BarCollection.Add(new List<BarChart.Bar>());
            multipleBarEntity.BarCollection[0].Add(new BarChart.Bar("string", "100"));
            multipleBarEntity.BarCollection[0].Add(new BarChart.Bar("connection", "50"));
            multipleBarEntity.BarCollection[0].Add(new BarChart.Bar("datatable", "10"));


            multipleBarEntity.BarCollection.Add(new List<BarChart.Bar>());
            multipleBarEntity.BarCollection[1].Add(new BarChart.Bar("string", "90"));
            multipleBarEntity.BarCollection[1].Add(new BarChart.Bar("connection", "40"));
            multipleBarEntity.BarCollection[1].Add(new BarChart.Bar("datatable", "20"));

            multipleBarEntity.BarCollection.Add(new List<BarChart.Bar>());
            multipleBarEntity.BarCollection[2].Add(new BarChart.Bar("string", "190"));
            multipleBarEntity.BarCollection[2].Add(new BarChart.Bar("connection", "140"));
            multipleBarEntity.BarCollection[2].Add(new BarChart.Bar("datatable", "120"));

            report.Entities.Add(multipleBarEntity);
        }

        private void InsertPieEntity(ChartFactory factory, Report report)
        {
            PieChart pieEntity = factory.CreatePieChart();
            pieEntity.Title = "Pie Chart1";
            pieEntity.Pies.Add(new PieChart.Pie("Heap1", "100"));
            pieEntity.Pies.Add(new PieChart.Pie("Heap2", "50"));
            pieEntity.Pies.Add(new PieChart.Pie("Heap3", "40"));
            pieEntity.Pies.Add(new PieChart.Pie("Heap4", "50"));
            pieEntity.Pies.Add(new PieChart.Pie("Heap5", "20"));
            pieEntity.Pies.Add(new PieChart.Pie("Heap6", "10"));

            report.Entities.Add(pieEntity);
        }

        #endregion
    }
}
